package com.lphl.pet.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.lphl.pet.GloableParams;
import com.lphl.pet.R;
import com.lphl.pet.adapter.MyPagerAdapter;
import com.lphl.pet.base.BasePager;
import com.lphl.pet.pager.DayPager;
import com.lphl.pet.pager.MonPager;
import com.lphl.pet.pager.WeekPager;

public class PetAnalyzeActivity extends Activity implements OnClickListener {

    private ImageView iv_back;
    private ImageView iv_down_icon;
    private TextView tv_share;
    private ViewPager viewPager;
    private ArrayList<BasePager> pagers;

    // 记录上一个位置
    private int lastPostion = 0;
    private ImageView underline;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_pet_analyze);

        initView();
        initPager();
        initUnderline();
        setListener();

        MyPagerAdapter pagerAdapter = new MyPagerAdapter(PetAnalyzeActivity.this, pagers);
        viewPager.setAdapter(pagerAdapter);
    }

    private void initView() {
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_down_icon = (ImageView) findViewById(R.id.iv_down_icon);
        tv_share = (TextView) findViewById(R.id.tv_share);
        viewPager = (ViewPager) findViewById(R.id.ii_vp);
        underline = (ImageView) findViewById(R.id.ii_category_selector);

        iv_back.setOnClickListener(this);
        iv_down_icon.setOnClickListener(this);
        tv_share.setOnClickListener(this);
    }

    /**
     * 初始化ViewPager下划线
     */
    private void initUnderline() {
        Bitmap bitmap = BitmapFactory.decodeResource(
                PetAnalyzeActivity.this.getResources(),
                R.drawable.id_category_selector);
        Matrix matrix = new Matrix();
        int offset = (GloableParams.winWidth / 3 - bitmap.getWidth()) / 2;
        matrix.postTranslate(offset, 0);
        underline.setImageMatrix(matrix);
    }
    /**
     * 添加ViewPager对象
     */
    private void initPager() {
        pagers = new ArrayList<BasePager>();
        pagers.add(new DayPager(PetAnalyzeActivity.this));
        pagers.add(new WeekPager(PetAnalyzeActivity.this));
        pagers.add(new MonPager(PetAnalyzeActivity.this));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                this.finish();
                break;

            case R.id.iv_down_icon:

                break;

            case R.id.tv_share:

                break;

            default:
                break;
        }
    }

    /**
     * 设置ViewPager滑动状态监听
     */
    protected void setListener() {
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                TranslateAnimation animation = new TranslateAnimation(
                        lastPostion * GloableParams.winWidth / 3, position
                        * GloableParams.winWidth / 3, 0, 0);
                animation.setDuration(300);
                animation.setFillAfter(true);
                underline.startAnimation(animation);
                lastPostion = position;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }
}
